/***************************************************************************
                          parser.h  -  description
                             -------------------
    begin                : Mon May 6 2002
    copyright            : (C) 2002 by Michiel Visser
    email                :
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef PARSER_H
#define PARSER_H

#include "expr.h"
#include <vector>

using namespace Expression;
using namespace  std;

class TyleParser
{
public:
	TyleParser(const unsigned char *pos, EPtr &result, void *returnObjectSpace);
		static const unsigned char *parseInt(const unsigned char *p, int &base, /*out*/ int &result); // returns zero if no integer parsed. result is zero.
private:
	short    m_c;
	int      m_line;
	const unsigned char *   m_pos; 
	int m_strlen;
	int m_dynamic;
	String   m_s;
	struct {
		List<TypeFunc>::P funcs;
	} structing;
	vector<bool> m_sharedLiteralDefined;
	vector<bool> m_sharedLiteralUnreferenced;
	void addStructElem(List<StructElem> &ses, TypeVar parent, String name, TypeVar type, TypeFunc *func);
	void structing_storeTxt(const char *s);
	void storeChar(int &pos) { m_s[pos++]=(unsigned char)m_c; }
	void nextRawChar()
	{	m_c=*(m_pos++);
	if (m_c==10) m_line++;
	}
	void nextChar();
	void skipSpaces()
	{  while (m_c>0 && m_c<=' ') nextChar();
	}
	void nextWord();
	bool parseWord(const char *word);
	static int parseDigits(const unsigned char *p, int &result, int base=10);
	void consolidateSharedLiterals(EPtr &p);
	void operCheck(EPtr &o) { if (!o) throw("Missing Operand."); }
	void pcheck(EPtr &o) { operCheck(o); consolidateSharedLiterals(o); }
	EPtr finishParseList();
	EPtr finishParseSequence();
	EPtr finishMap();
	TypeVar parseType       (/*out*/ TypeFunc *&funcOut);
	TypeVar parseTypeMapping(/*out*/ TypeFunc *&funcOut);
	TypeVar parseTypeLeftVal(/*out*/ TypeFunc *&funcOut);
	TypeVar parseTypeLiteral  (/*out*/ TypeFunc *&funcOut);
	TypeVar parseTypeAssignment(/*out*/ TypeFunc *&funcOut);
	TypeVar parseTypeSelection(/*out*/ TypeFunc *&funcOut, bool *pFound=0);
	TypeVar parseStructConcat (/*out*/ TypeFunc *&funcOut);
	EPtr parseBinary(int level=100);
	EPtr parseBinaryOp(EPtr cur, int level);
#define DEF_PARSEFUNC
#define P(x,l) EPtr parse##x();
	#include"parseunits.h"
#undef P
#undef DEF_PARSEFUNC
#define parseExpression parseAssign
	EPtr parseSequence();
	EPtr parseTypeSelectionExpr();
	EPtr parseIndependentBlock();
};

#endif // FILEXXX_H

